//inSingnUp.js
const app = getApp()

Page({
  data: {

  },
  onLoad: function () {
    var that = this
    /**
     * 时间格式化
     */
    function transDate(mescStr) {
      var n = mescStr;
      var date = new Date(n);
      var Y = date.getFullYear() + '-';
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
      return (Y + M + D)
    }
    /**
     * 请求进行中的任务数据
     */
    wx.request({
      url: app.globalData.API_URL + "signUp/getInSignUp",
      data: {
        token: app.globalData.token
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'post',
      success: function (r) {
        if (r.data.code == 0) {
          that.setData({
            inSignUpData: r.data.data,
            createTime: transDate(r.data.data.createTime),
            beginTime: transDate(r.data.data.beginTime),
            endTime: transDate(r.data.data.endTime),
            time: r.data.data.endTime
          })
        }
      }
    })
  },

  /**
   * 申请退出
   */
  clickEndsignUp: function () {
    var that = this
    if (that.data.time - Date.parse(new Date()) < 0) {
      wx.request({
        url: app.globalData.API_URL + "signUp/finishSignUp",
        data: {
          token: app.globalData.token
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'post',
        success: function (r) {
          if (r.data.code == 0) {
            app.globalData.myDataHaveChange = true;
            app.globalData.appGetDataFinish = false;
            app.getHomeData();
            wx.showToast({
              title: '退出成功！',
              icon: 'success',
              duration: 2000
            })
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/my/my'
              })
            }, 2000)
          } else {
            console.log(r.data.msg);
          }
        }
      })
    } else {
      wx.showToast({
        title: '目标未结束',
        image: '/images/toast_icon_warn.png',
        duration: 2000
      })
    }
  },
})